package com.shengwei.truck.driver.view.ui.app;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.permission.RequestPermissionResult;
import com.shengwei.truck.driver.permission.RxjavaPermissionUtil;
import com.shengwei.truck.driver.view.ui.mine.userinfo.LoginActivity;
import com.tbruyelle.rxpermissions2.Permission;
/**
 * 开屏页
 */
public class SplashActivity extends AppCompatActivity implements RequestPermissionResult {
    private static final int sleepTime = 500;
    ImageView ivSplash;
    private static final int REQUEST_CODE_OPEN_GPS = 1;
    String TAG = "SplashActivity";
    @Override
    protected void onCreate(Bundle arg0) {
        setStatusBar();
        setContentView(R.layout.activity_splash);
        super.onCreate(arg0);

        String[]permissionGroup = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
        };
        RxjavaPermissionUtil.getInstance().checkPermissions(this,permissionGroup);
    }
    /**
     * 设置沉浸式状态栏
     * 在添加布局文件之前
     */
    public void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            //有底部导航栏的手机 虚拟home键 如果加上下面一句 底部的控制栏看不清了
            //window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }
    private boolean checkGPSIsOpen() {
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager == null)
            return false;
        return locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
    }

    public void startAnimation()
    {
        AlphaAnimation animation = new AlphaAnimation(0.3f, 1.0f);
        animation.setDuration(1500);
        ivSplash = this.findViewById(R.id.imvLogo);
        ivSplash.startAnimation(animation);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                }
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                finish();
            }
        }).start();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_OPEN_GPS) {
            startAnimation();
        }
    }
    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 权限被允许了
     * @param permission：被允许的权限
     */
    @Override
    public void onPermissionGranted(Permission permission) {
            switch (permission.name) {  //gxw:拥有了定位权限还不够，需要继续打开GPS服务开关
                case Manifest.permission.ACCESS_FINE_LOCATION:
                    startAnimation();
                    break;
            }

    }
    /**
     * 权限被拒绝了
     * @param DeniedType =1 表示下次还提醒，2再也不提醒了
     */
    @Override
    public void onPermissionDenied(Permission permission, int DeniedType) {
        startAnimation();
    }



}
